﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;

namespace DrawToolsLib {
    public sealed class DrawingTools : DependencyObject {
        public static readonly DependencyProperty ToolProperty;
        public static DrawingTools MyDrawingTools;

        private Tool[] tools;                   // Array of tools

        DrawingTools() {
            // create array of drawing tools
            tools = new Tool[(int)ToolType.Max];

            tools[(int)ToolType.Pointer] = new ToolPointer();
            tools[(int)ToolType.Rectangle] = new ToolRectangle();
            tools[(int)ToolType.Ellipse] = new ToolEllipse();
            tools[(int)ToolType.PolyLine] = new ToolPolyLine();
            tools[(int)ToolType.Polygon] = new ToolPolygon();
            tools[(int)ToolType.Text] = new ToolText();   // kept as class member for in-place editing
            toolType = ToolType.Pointer; 
        }

        static DrawingTools() {
            // Tool
            PropertyMetadata metaData = new PropertyMetadata(ToolType.Pointer);

            ToolProperty = DependencyProperty.Register(
                "toolType", typeof(ToolType), typeof(DrawingCanvas),
                metaData);

            MyDrawingTools = new DrawingTools();   
        }

        #region Tool

        /// <summary>
        /// Currently active drawing tool
        /// </summary>
        private ToolType toolType {
            get {
                return (ToolType)GetValue(ToolProperty);
            }
            set {
                if ((int)value >= 0 && (int)value < (int)ToolType.Max) {
                    SetValue(ToolProperty, value);
                    // Set cursor immediately - important when tool is selected from the menu
                    //tools[(int)Tool].SetCursor(this);
                }
            }
        }

        public static ToolType ToolType {
            get {
                return MyDrawingTools.toolType;
            }
            set {
                MyDrawingTools.toolType = value;
            }
        }

        internal static Tool Tool {
            get {
                return MyDrawingTools.tools[(int)MyDrawingTools.toolType];
            }
        }

        #endregion Tool
    }
}
